﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Error.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Ssl/NetClientSsl_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Ssl/NetClientSsl_Error.h"

//g++ -std=gnu++11 -Wall -g Test_SSLClient.cpp -o Test_SSLClient -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client -lNetEngine_BaseLib -lNetClient_Socket -lNetClient_Ssl -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client,--disable-new-dtags

int main()
{
    XNETHANDLE xhNet;
    SOCKET m_Socket;
    NETCLIENT_OPENSSL_SRVINFO st_SrvInfo;
    memset(&st_SrvInfo, '\0', sizeof(NETCLIENT_OPENSSL_SRVINFO));

    LPCTSTR lpszCAFile = _T("./ca.crt");
    LPCTSTR lpszFile = _T("./client.crt");
    LPCTSTR lpszKeyFile = _T("./client.key");
    if (!NetClient_OPenSsl_InitEx(&xhNet, NETCLIENT_ENUM_OPENSSL_SSL_VERSION, lpszCAFile, lpszFile,lpszKeyFile))
    {
        printf("NetClient_OpenSsl_Init:%lX\n", NetClientSsl_GetLastError());
        return -1;
    }
    if (!NetClient_TCPSelect_Create("192.168.1.4", 55443, &m_Socket))
    {
        printf("NetClient_TCPSelect_Create:%lX\n", NetClient_GetLastError());
        return -1;
    }
    if (!NetClient_OPenSsl_ConnectEx(xhNet, m_Socket, &st_SrvInfo))
    {
        printf("NetClient_OPenSsl_ConnectEx:%lX\n", NetClientSsl_GetLastError());
        return -1;
    }
    std::string strWrite =
        "GET https://www.baidu.com/ HTTP/1.1\r\n"
        "Host: www.baidu.com\r\n"
        "Connection: close\r\n\r\n";

    if (!NetClient_OPenSsl_SendMsgEx(xhNet, strWrite.c_str(), strWrite.length()))
    {
        printf("NetClient_OpenSsl_SendMsg:%lX\n", NetClientSsl_GetLastError());
        return -1;
    }
    while (1)
    {
        TCHAR tszMsgBuffer[2048];
        int nLen = 2048;

        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

        if (NetClient_TCPSelect_IoSelect(m_Socket))
        {
            if (!NetClient_OPenSsl_RecvMsgEx(xhNet, tszMsgBuffer, &nLen))
            {
                break;
            }
            printf("%s\n", tszMsgBuffer);
        }

    }
    NetClient_OPenSsl_CloseEx(xhNet);
    NetClient_TCPSelect_Close(m_Socket);
    return 0;
}
